<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item>
          <el-button type="primary" @click="getlist">
            <i-ep-search />
            搜索
          </el-button>
          <el-button @click="getlist">
            <i-ep-refresh />
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" plain @click="openDialog()">
          <i-ep-plus />
          新增
        </el-button>
        <el-button
          type="danger"
          plain
          :disabled="ids.length === 0"
          @click="handleTableDelete()"
        >
          <i-ep-delete />
          删除
        </el-button>
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tabledataList"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
        row-key="id"
        :indent="30"
        default-expand-all
        :tree-props="{
          children: 'nodes',
          hasChildren: 'hasChildren',
        }"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="目录名称"
          align="left"
          prop="directoryName"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="目录编号"
          prop="directoryCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="备注"
          prop="remarks"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column label="是否启用" prop="enabled" min-width="100">
          <template #default="scope">
            {{ useDictionary("Enabled", scope.row.enabled) }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="280">
          <template #default="scope">
            <el-button
              type="success"
              size="small"
              link
              @click="openDialog(scope.row, 'child')"
            >
              <i-ep-plus />
              添加数据目录
            </el-button>
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)"
            >
              <i-ep-edit />
              编辑
            </el-button>
            <el-button
              type="primary"
              size="small"
              link
              @click="handleTableDelete(scope.row.id)"
            >
              <i-ep-delete />
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <dmpdatareportdirectory-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      :parentId="addDialog.parentId"
      @reset-table="(id:string) => (id?.length ? getTableList() : getlist())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";

defineOptions({
  name: "DMPDATAREPORTDIRECTORY",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/dmpAdmin/dmpdatareportdirectory/treeList";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {};

const deleteParam = {
  api: "/dmpAdmin/dmpdatareportdirectory/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

const tabledataList = ref([]);

onMounted(() => {
  getlist();
});

function getlist(id: string) {
  request({
    url: requestApi,
    method: "get",
  })
    .then((data) => {
      tabledataList.value = data.data;
    })
    .catch((res) => {
      console.log(res);
    });
}

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
  parentId: "0",
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/dmpAdmin/dmpdatareportdirectory/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.data.enabled = addDialog.data.enabled.toString();
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开数据报表目录表单弹窗 */
function openDialog(roleObj?: any, type?: any) {
  if (roleObj && !type) {
    // 编辑
    addDialog.title = `【${roleObj.directoryName}】修改数据报表目录`;
    getInfo(roleObj.id);
    addDialog.parentId = roleObj.parentId;
  } else if (roleObj && type) {
    // 添加子机构
    addDialog.title = `【${roleObj.directoryName}】添加子数据报表目录`;
    addDialog.parentId = roleObj.id;
    console.log(addDialog.data);
    addDialog.visible = true;
  } else {
    addDialog.data = {};
    addDialog.title = "新增数据报表目录";
    addDialog.visible = true;
    addDialog.parentId = "0";
  }
}

/** 打开数据报表目录表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "数据报表目录详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}
</script>
